/***************************************************************************
 *   This file is part of KTagger                                          *
 *   Copyright (C) 2010 by Luca Bellonda                                   *
 *   lbellonda _at_ gmail.com                                              *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#define KTAGGER_APPLET_SOURCE

#include "KTaggerApplet.h"
#include <KIcon>
#include <climits>

#include <QDBusInterface>
#include <QDBusReply>
#include <QGraphicsLinearLayout>
#include <QPushButton>

#include <plasma/corona.h>
#include <plasma/widgets/iconwidget.h>

#include <QGraphicsSceneHoverEvent>
#include <QGraphicsGridLayout>
//#include <QPropertyAnimation>

#include <Plasma/Applet>
#include <Plasma/Animator>
#include <Plasma/DataEngine>
#include <Plasma/PopupApplet>
#include <Plasma/IconWidget>

#include "ktagger_interface.h"

#define SIZE_CMIN 16
#define SIZE_CPREF 32
#define SIZE_CMAX 64
#define SPACING 8

#define SOURCE_CLASS "KTaggerApplet"

#include "KTaggerAppletPrivate.h"

TLogger logger(true, KTAGGERAPPLET_LOG_TAG, "/tmp/ktagapplet.log") ;

KTaggerApplet::KTaggerApplet(QObject * parent, const QVariantList &args):
        Plasma::Applet(parent, args), d(new Private(this))
{
    initLog();
    logger.debug(&logParams, "started");
}

KTaggerApplet::~KTaggerApplet()
{
    delete d ;
    logger.debug(&logParams, "end");
}

void KTaggerApplet::initLog()
{
    logParams.source = SOURCE_CLASS;
}

void KTaggerApplet::init()
{
    KGlobal::locale()->insertCatalog(CATALOG_NAME);

    qDebug() << "PlasmaTagApplet::init()";
    logger.debug(&logParams, "init");
    setBackgroundHints(DefaultBackground);
    d->init();

    if (containment() != NULL) {
        logger.error(&logParams, QString("Attivita:%1").arg(containment()->activity()));
    }
    d->setCurrentActivity(getCurrentActivityName());
    connect(context(), SIGNAL(activityChanged(Plasma::Context *)), this, SLOT(activityChanged(Plasma::Context *)));

    setAcceptDrops(true);
}

QString KTaggerApplet::getCurrentActivityName()
{
    if (containment() != NULL) {
        return containment()->activity() ;
    }
    return "";
}

void KTaggerApplet::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
{
    if (d->canDoOperation()) {
        event->setAccepted(true);
        Applet::dragEnterEvent(event);
    }
}

void KTaggerApplet::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
{
}

void KTaggerApplet::dragMoveEvent(QGraphicsSceneDragDropEvent *event)
{
    event->accept();
}

void KTaggerApplet::dropEvent(QGraphicsSceneDragDropEvent *event)
{
    d->assignClicked();

    event->accept();
}

QList<QAction*> KTaggerApplet::contextualActions()
{
    return d->getContextualActions();
}


void KTaggerApplet::activityChanged(Plasma::Context *context)
{
    d->setCurrentActivity(getCurrentActivityName());
}


#include "KTaggerApplet.moc"

